'use strict';
//服务粉丝
import React,{Component} from 'react';
import Header from '../../common/header';
import * as URLconstant from '../../constant/URLconstant'
import Util from '../../common/util';
import {
    View,
    Text,
    Image,
    StyleSheet,
    ListView
} from 'react-native';
export default class serviceFans extends Component{
    // 构造
      constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            page:1,
            isOver:false,
            dataSource2: new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2}),
            dataSource: [],
            loaded:true
        };
        this.onEndReached=this.onEndReached.bind(this)
          this.renderFooter=this.renderFooter.bind(this)
          this._renderRow=this._renderRow.bind(this)
      }

    componentDidMount() {
        this.getFans()
    }

      getFans(){
          let url= URLconstant.SERVICE_FANS
          let datakey=[]
          datakey['user_id']=this.props.user_id
          datakey['allShop']=0
          datakey['page']=this.state.page
          Util.get(url,datakey,this.getFansSuc.bind(this),this.errorCallback.bind(this))
      }

    getFansSuc(datas){
        if(datas.status==0){
            let newList = datas.data.memberList
            let limit = datas.data.listRows
            let isOver = false
            if(newList.length<limit){
                isOver=true
            }
            let oldDatas = this.state.dataSource
            Array.prototype.push.apply(oldDatas, newList);
            this.setState({
                isOver:isOver,
                dataSource2: this.state.dataSource2.cloneWithRows(oldDatas),
                dataSource: oldDatas,
                loaded:false,
                page:this.state.page+1
            })
        }
    }
    errorCallback(err){

    }

    onEndReached(){
        let isOver = this.state.isOver
        if(!isOver){
            this.getFans()
        }
    }

    renderFooter(){
        return (
            <View style={{height:30,alignItems:'center',justifyContent:'center'}}>
                <Text style={{fontSize:12,color:"#969696"}}>{this.state.isOver?"没有更多数据了":"正在加载中"}</Text>
            </View>
        )
    }

    _renderRow(item){
          return(
              <View style={styles.fans_item} key={item.id}>
                  <Image
                      source={require('../../imgs/user.png')}
                      resizeMode={'cover'}
                      style={{width:90,height:90}}
                  />
                  <View style={styles.item_right}>
                      <View style={styles.txt_view}>
                          <Text style={styles.txt_1}>昵称:</Text>
                          <Text style={styles.txt_2}>{item.user_nickname}</Text>
                      </View>
                      <View style={styles.txt_view}>
                          <Text style={styles.txt_1}>当前余额:</Text>
                          <Text style={styles.txt_3}>{item.sum_account}</Text>
                      </View>
                      <View style={styles.txt_view}>
                          <Text style={styles.txt_1}>累计充值:</Text>
                          <Text style={styles.txt_3}>{item.sum_charge_amount}</Text>
                      </View>
                      <View style={styles.txt_view}>
                          <Text style={styles.txt_1}>累计消费:</Text>
                          <Text style={styles.txt_3}>{item.sum_order_amount}</Text>
                      </View>
                      <View style={styles.txt_view}>
                          <Text style={styles.txt_1}>加入时间:</Text>
                          <Text style={styles.txt_2}>{this.backDate(item.add_time)}</Text>
                      </View>
                  </View>
              </View>
          )
    }


    backDate(add_time){
        let date = new Date(add_time*1000)
        let year = date.getFullYear() + '-';
        let month = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
        let hours = (date.getHours()<10?'0'+date.getHours():date.getHours())+":"
        let minutes = (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes())+":"
        let seconds = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds()
        let timeTxt = year+month+day+hours+minutes+seconds
        return timeTxt
    }

      render(){
          return(
              <View style={{flex:1,backgroundColor:"#f0f0f0"}}>
                  <Header navigator={this.props.navigator} initObj={{ title: '服务粉丝',id:2}}/>
                  {
                      this.state.loaded?
                          <View style={{flex:1}}>
                              {Util.loading()}
                          </View>
                          :
                          <View style={{flex:1}}>
                              {
                                  this.state.dataSource.length==0?
                                      <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
                                          <Text>没有服务粉丝</Text>
                                      </View>:
                                      <ListView
                                          dataSource={this.state.dataSource2}
                                          onEndReached={this.onEndReached}
                                          renderRow={this._renderRow}
                                          renderFooter={this.renderFooter}
                                      />
                              }
                          </View>
                  }
              </View>
          )
      }
}
var styles = StyleSheet.create({
    fans_item:{
        padding:7,
        flexDirection:'row',
        backgroundColor:"#fff",
        marginBottom:10
    },
    item_right:{
        flex:1,
        height:90,
        justifyContent:'space-between',
        paddingLeft:10
    },
    txt_view:{
        flexDirection:'row',
        alignItems:'center',
    },
    txt_1:{
        fontSize:12,
        color:"#969696"
    },
    txt_2:{
        fontSize:12,
        color:"#000"
    },
    txt_3:{
        fontSize:12,
        color:"#f00"
    }
})